package com.x.erp.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.x.erp.dao.PriceDao;
import com.x.erp.dao.SaleBillDao;
import com.x.erp.model.Price;
import com.x.erp.model.SaleBill;
import com.x.erp.rpc.SaleBillRpcService;
import com.x.framework.exception.BusinessException;
import com.x.framework.exception.ExceptionCode;
import com.x.framework.service.BaseService;

/**
 * 销售单据服务
 */
@Component
@Service(interfaceName = "com.x.erp.rpc.SaleBillRpcService", timeout = 60000)
public class SaleBillService extends BaseService implements SaleBillRpcService {

	@Autowired
	private SaleBillDao saleBillDao;// 销售单据Dao

	@Autowired
	private PriceDao priceDao;// 价格Dao

	/**
	 * 查询条件查询结果数量
	 * 
	 * @param saleBill
	 * @return
	 */
	@Override
	public int selectSaleBillNum(SaleBill saleBill) {
		return saleBillDao.selectSaleBillNum(saleBill);
	}

	/**
	 * 查询单据列表
	 * 
	 * @param saleBill
	 * @return
	 */
	@Override
	public List<SaleBill> selectSaleBillList(SaleBill saleBill) {
		try {
			return saleBillDao.selectSaleBillList(saleBill);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 插入单据信息
	 * 
	 * @param saleBill
	 * @return
	 */
	@Override
	public SaleBill insertSaleBill(SaleBill saleBill) {
		try {
			// 根据价格查询价格信息
			Price price = priceDao.selectPriceInfoByPrice(saleBill.getBillPrice());
			saleBill.setPriceId(price.getPriceId());
			return this.insert(saleBill);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 修改单据信息
	 * 
	 * @param saleBill
	 */
	@Override
	public void updateSaleBill(SaleBill saleBill) {
		try {
			// 根据价格查询价格信息
			Price price = priceDao.selectPriceInfoByPrice(saleBill.getBillPrice());
			saleBill.setPriceId(price.getPriceId());
			this.update(saleBill);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 删除单据信息
	 * 
	 * @param saleBill
	 */
	@Override
	public void deleteSaleBill(SaleBill saleBill) {
		this.delete(saleBill);
	}

	/**
	 * 查询今日成交信息
	 * 
	 * @param saleBill
	 * @return
	 */
	@Override
	public Map<String, Object> selectTodaySaleInfo(SaleBill saleBill) {
		try {
			return saleBillDao.selectTodaySaleInfo(saleBill);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询本月成交信息
	 * 
	 * @param saleBill
	 * @return
	 */
	@Override
	public Map<String, Object> selectThisMonthSaleInfo(SaleBill saleBill) {
		try {
			return saleBillDao.selectThisMonthSaleInfo(saleBill);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询累计成交信息
	 * 
	 * @param saleBill
	 * @return
	 */
	@Override
	public Map<String, Object> selectTotalSaleInfo(SaleBill saleBill) {
		try {
			return saleBillDao.selectTotalSaleInfo(saleBill);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 条件查询成交信息
	 * 
	 * @param saleBill
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	@Override
	public Map<String, Object> selectSaleInfoByCondition(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectSaleInfoByCondition(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询市场销售情况
	 * 
	 * @param saleBill
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectMarketSaleInfo(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectMarketSaleInfo(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询商铺销售排行
	 * 
	 * @param saleBill
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectShopSaleRank(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectShopSaleRank(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询销售额排行
	 * 
	 * @param saleBill
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectSalesVolumeRank(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectSalesVolumeRank(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询畅销品牌排行
	 * 
	 * @param saleBill
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectBrandRank(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectBrandRank(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询车型排行
	 * 
	 * @param saleBill
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectModelRank(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectModelRank(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询颜色排行
	 * 
	 * @param saleBill
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            结束时间
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectColorRank(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectColorRank(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询区域销售信息
	 * 
	 * @param saleBill
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectAreaSaleInfo(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectAreaSaleInfo(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询价格排行
	 * 
	 * @param saleBill
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectPriceRank(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectPriceRank(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

	/**
	 * 查询月销售信息
	 * 
	 * @param saleBill
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@Override
	public List<Map<String, Object>> selectMonthSaleInfo(SaleBill saleBill, Date startDate, Date endDate) {
		try {
			return saleBillDao.selectMonthSaleInfo(saleBill, startDate, endDate);
		} catch (Exception e) {
			throw new BusinessException(ExceptionCode.EXCEPTION_CODE0000, e);
		}
	}

}
